<template>
	<view>
		<button @click="handleDoLogin(true)">确认登录</button>
		<button @click="handleDoLogin(false)">取消登录</button>
	</view>
</template>

<script setup>
	import {onLoad} from '@dcloudio/uni-app';
	import {ref} from 'vue';
	const toInfo = ref();
	const username = '刘军';
	onLoad((option) => {
		const {to} = option;
		toInfo.value = to;
		uni.request({
			url: 'http://localhost:5269/api/login_code',
			method: 'POST',
			data: {
				isCode: true,
				to
			},
		})
	})
	
	function handleDoLogin(flag) {
		if(!flag) {
			uni.request({
				url: 'http://localhost:5269/api/login_code',
				method: 'POST',
				data: {
					isCode: flag,
					to: toInfo.value
				},
				success() {
					uni.navigateBack()
				}
			})
		} else {
			uni.request({
				url: 'http://localhost:5269/api/login',
				method: 'POST',
				data: {
					username,
					to: toInfo.value,
					mode: 'CODE'
				},
				success: (res) => {
					uni.navigateBack()
				},
				fail() {
					console.log('出错了')
				}
			})
		}
	} 
</script>

<style>

</style>
